package com.kpmg.questionnaire.controller;

import com.kpmg.questionnaire.model.QuestionEntity;
import com.kpmg.questionnaire.model.QuestiontableEntity;
import com.kpmg.questionnaire.model.UserEntity;
import com.kpmg.questionnaire.repository.QuestionRepository;
import com.kpmg.questionnaire.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by xionghao on 2016/11/9.
 */
@Controller
public class QuestionController {
    // 自动装配数据库接口，不需要再写原始的Connection来操作数据库
    @Autowired
    private QuestionRepository userRepository;


    @Autowired
    private QuestionRepository questionRepository;

//    文件调查页面
    @RequestMapping(value = "/question", method = RequestMethod.GET)
    public String showQuestion(ModelMap modelMap){
//        List<QuestionEntity> questionList = questionRepository.findAll();
        List<QuestionEntity> list = questionRepository.findAll();
        modelMap.addAttribute("list", list);
        return "question/question";
    }


    // 根据问卷ID查找问题
    @RequestMapping(value = "/question/list/{itemId}", method = RequestMethod.GET)
    public String showList(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, @PathVariable("itemId") Integer itemId){

        List<QuestionEntity> list = questionRepository.findAll();
//        List<UserEntity> user = request.getSession().getAttribute("SESSION_USER_KEY");
//        modelMap.addAttribute("user", );
        modelMap.addAttribute("list", list);

        return "question/list";
    }

    // 添加问卷
    @RequestMapping(value = "/question/addItem", method = RequestMethod.GET)
    public String addItem(){
        return "question/addItem";
    }

    // 添加处理
    @RequestMapping(value = "/question/addItemPost", method = RequestMethod.POST)
    public String addItemPost(@ModelAttribute("question") QuestionEntity questionEntity){
        // 向数据库添加一个用户
        //userRepository.save(userEntity);

        // 向数据库添加一个表单，并将内存中缓存区的数据刷新，立即写入数据库，之后才可以进行访问读取
        questionRepository.saveAndFlush(questionEntity);

        // 返回重定向页面
        return "redirect:/question";
    }

    // 查看用户详细信息
    // @PathVariable可以收集url中的变量，需匹配的变量用{}括起来
    // 例如：访问 localhost:8080/showUser/1 ，将匹配 userId = 1
    @RequestMapping(value = "/question/showItem/{itemId}", method = RequestMethod.GET)
    public String showItem(@PathVariable("itemId") Integer itemId, ModelMap modelMap ){
        // UserEntity userEntity = userRepository.findOne(userId);
        QuestionEntity item = questionRepository.findOne(itemId);
        modelMap.addAttribute("item", item);
        return "question/itemDetail";
    }
    // 更新信息页面
    @RequestMapping(value = "/question/updateItem/{itemId}", method = RequestMethod.GET)
    public String updateItem(@PathVariable("itemId") Integer itemId, ModelMap modelMap){
        QuestionEntity item = questionRepository.findOne(itemId);
        modelMap.addAttribute("item", item);
        return "question/updateItem";
    }

    // 处理用户修改请求
    @RequestMapping(value = "/question/updateItemPost", method = RequestMethod.POST)
    public String updateItemPost(@ModelAttribute("question") QuestionEntity questionEntity){
        questionRepository.updateItem(
                questionEntity.getContent(),
                questionEntity.getType(),
                questionEntity.getImpact(),
                questionEntity.getChoose1(),
                questionEntity.getChoose2(),
                questionEntity.getChoose3(),
                questionEntity.getChoose4(),
                questionEntity.getChoose5(),
                questionEntity.getSurvey(),
                questionEntity.getScore(),
                questionEntity.getNote(),
                questionEntity.getId()
        );
        return "redirect:/question";
    }

    // 删除用户
    @RequestMapping(value = "/question/deleteItem/{itemId}", method = RequestMethod.GET)
    public String deleteQuestion(@PathVariable("itemId") Integer itemId){
        // 删除id为userId的用户
        questionRepository.delete(itemId);
        // 立即刷新数据库
        questionRepository.flush();
        return "redirect:/question";
    }

//

}
